<template>
  <div class="chart-container">
    <div class="chart-header">
      <h3>热门图书分析</h3>
      <div class="chart-controls">
        <el-select v-model="category" size="small" style="min-width: 120px">
          <el-option label="全部分类" value="" />
          <el-option label="总裁豪门" :value="1" />
          <el-option label="古代情缘" :value="12" />
          <el-option label="末世求生" :value="20" />
          <el-option label="青春校园" :value="6" />
          <el-option label="宫闱宅斗" :value="11" />
        </el-select>
      </div>
    </div>
    <div ref="chartRef" class="chart-content"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue'
import * as echarts from 'echarts'
import { getPopularBooks } from '@/api/statistics'

const props = defineProps({
  startDate: {
    type: String,
    required: true
  },
  endDate: {
    type: String,
    required: true
  }
})

const chartRef = ref(null)
let chart = null
const category = ref('')

const initChart = async () => {
  await nextTick()
  if (!chartRef.value) return
  
  if (chart) {
    chart.dispose()
  }
  
  chart = echarts.init(chartRef.value)
  await updateChart()
}

const updateChart = async () => {
  if (!chart) return
  
  const params = {
    startDate: props.startDate,
    endDate: props.endDate,
    category: category.value
  }
  
  try {
    const res = await getPopularBooks(params)
    if (res.code === 0 && res.data) {
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01]
        },
        yAxis: {
          type: 'category',
          data: res.data.books.reverse()
        },
        series: [
          {
            name: '书籍热度',
            type: 'bar',
            data: res.data.borrowCount.reverse(),
            itemStyle: {
              color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [
                { offset: 0, color: '#83bff6' },
                { offset: 0.5, color: '#188df0' },
                { offset: 1, color: '#188df0' }
              ])
            },
            label: {
              show: true,
              position: 'right',
              formatter: '{c} 热度'
            }
          }
        ]
      }
      
      chart.setOption(option)
    }
  } catch (error) {
    console.error('更新图表失败:', error)
  }
}

const handleFiltersChange = async () => {
  await updateChart()
}

watch([() => props.startDate, () => props.endDate, category], () => {
  handleFiltersChange()
})

const handleResize = () => {
  chart?.resize()
}

onMounted(async () => {
  await initChart()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  if (chart) {
    chart.dispose()
    chart = null
  }
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.chart-container {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  width: 100%;
  height: 100%;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 16px;
  padding-right: 16px;
}

.chart-controls {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.chart-content {
  width: 100%;
  height: calc(100% - 80px);
  min-height: 500px;
}

h3 {
  margin: 0;
  font-size: 18px;
  color: #303133;
  white-space: nowrap;
}

:deep(.el-space) {
  flex-wrap: wrap;
}

:deep(.el-select) {
  margin-left: 8px;
}

:deep(.el-space__item:first-child) .el-select {
  margin-left: 0;
}
</style> 